package com.lzk.org.service.impl;

import com.lzk.basic.service.impl.BaseServiceImpl;
import com.lzk.basic.util.Md5Utils;
import com.lzk.basic.util.StrUtils;
import com.lzk.org.domain.Employee;
import com.lzk.org.mapper.EmployeeMapper;
import com.lzk.org.service.IEmployeeService;
import com.lzk.user.domain.LoginInfo;
import com.lzk.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    /**
     * 重写保存操作
     * 添加时，同时添加employee_role表和logininfo表
     * @param employee
     */
    @Override
    @Transactional
    public void save(Employee employee) {
        //处理密码盐值
        //1.获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //2.获取加密后的密码 加密加盐
        String md5Password = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Password);
        //通过emp保存到logininfo
        LoginInfo loginInfo = employee2Logininfo(employee);

        //首先保存logininfo
        loginInfoMapper.save(loginInfo);

        //获取自增logininfo_id，再保存到employee
        employee.setLogininfo_id(loginInfo.getId());
        //保存emp
        employeeMapper.save(employee);

        //保存employee_role
        employeeMapper.saveEmployeeRole(employee);
    }


    /**
     * 更新员工表，同时更新employee_role表
     * @param employee
     */
    @Override
    @Transactional
    public void update(Employee employee) {
        //处理密码和盐值
        //1.获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //2.获取加密后的密码 加密加盐
        String md5Password = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Password);
        //通过emp保存到logininfo
        LoginInfo loginInfo = employee2Logininfo(employee);

        //获取当前的logininfo_id
        Long logininfo_id = employeeMapper.loadById(employee.getId()).getLogininfo_id();
        loginInfo.setId(logininfo_id);
        //更新id
        loginInfoMapper.update(loginInfo);

        //登录信息id和店铺信息id不能修改
        employeeMapper.update(employee);

        //删除中间表信息
        employeeMapper.removeEmployeeRole(employee.getId());
        //保存新的信息
        employeeMapper.saveEmployeeRole(employee);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        //先删除关联表
        Employee employee = employeeMapper.loadById(id);
        //删除登录信息表
        loginInfoMapper.remove(employee.getLogininfo_id());

        //删除中间表
        employeeMapper.removeEmployeeRole(id);
        //删除employee表数据
        employeeMapper.remove(id);
    }

    /**
     * 通过emp保存logininfo
     * @param employee
     * @return
     */
    private LoginInfo employee2Logininfo(Employee employee) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(employee,loginInfo);
        //当前对象是管理员
        loginInfo.setType(0);
        return loginInfo;
    }

    @Override
    public List<Employee> findAllByShopId(Long shop_id) {
        return employeeMapper.findAllByShopId(shop_id);
    }
}
